<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class operator extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('m_operator');
    }

    function index() {
        // $cek = $this->session->userdata('logged_in');
        // if (empty($cek)) {
        //     header('location:' . site_url('login'));
        // }
        $this->load->model('m_operator');
        $list_operator = $this->m_operator->get_all_operator();

        //ambil data atasan
        if ($list_operator->num_rows() > 0) {
            foreach ($list_operator->result() as $data_operator) {
                $data_operator_atasan[$data_operator->id_operator] = $this->m_operator->get_data_formasi_atasan($data_operator->id_operator);
            }
            $data['atasan'] = $data_operator_atasan;
        }

        //ambil data bawahan
        if ($list_operator->num_rows() > 0) {
            foreach ($list_operator->result() as $data_operator) {
                $data_operator_bawahan[$data_operator->id_operator] = $this->m_operator->get_data_formasi_bawahan($data_operator->id_operator);
            }
            $data['bawahan'] = $data_operator_bawahan;
        }

//        echo '<pre>';
//        print_r($data);
//        echo '</pre>';
        $data['list'] = $list_operator->result();
        $data['konten'] = 'v_operator';
        $this->load->view('theme', $data);
    }

    function tambah_operator() {
        $data['list'] = $this->m_operator->get_all_operator()->result();
        $data['konten'] = 'v_operator_tambah';
        $data['nama'] = '';
        $data['nik'] = '';
        $data['jabatan'] = '';
        $data['email'] = '';
        $data['atasan_lsg'] = '';
        $data['bawahan_lsg'] = '';
        $data['username'] = '';
        $data['password'] = '';
        $data['no_hp'] = '';
        $data['status'] = '';
        $this->load->view('theme', $data);
    }

    function fungsi_tambah_operator() {
        // $cek = $this->session->userdata('logged_in');
        // if (empty($cek)) {
        //     header('location:' . site_url('login'));
        // }
        $this->form_validation->set_rules('nama', 'Nama', 'trim|required');
        $this->form_validation->set_rules('nik', 'Nik', 'trim|required');
        $this->form_validation->set_rules('jabatan', 'Jabatan', 'trim|required');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
        $this->form_validation->set_rules('username', 'Username', 'trim|required');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]');
        $this->form_validation->set_rules('no_hp', 'Nomor HP', 'trim|required|numeric');
        $this->form_validation->set_rules('status', 'Status', 'trim|required');

        $data['nama'] = $this->input->post('nama');
        $data['nik'] = $this->input->post('nik');
        $data['jabatan'] = $this->input->post('jabatan');
        $data['email'] = $this->input->post('email');
//        $data['atasan_lsg'] = $this->input->post('atasan_lsg');
//        $data['bawahan_lsg'] = $this->input->post('bawahan_lsg');
        $data['username'] = $this->input->post('username');
        $data['password'] = $this->input->post('password');
        $data['no_hp'] = $this->input->post('no_hp');
        $data['status'] = $this->input->post('status');
        $cek_nik = $this->m_operator->cek_nik($data['nik']);
        if ($cek_nik == true) {
            if ($this->form_validation->run() == TRUE) {
                //jika nik benar
                $id_operator_baru = $this->m_operator->insert_operator($data);

                //insert data atasan
                    if (count($this->input->post('atasan_lsg')) > 0) {
                        foreach ($this->input->post('atasan_lsg') as $dt_atasan) {
                            $data_atasan['id_operator_atasan'] = $dt_atasan;
                            $data_atasan['id_operator_bawahan'] = $id_operator_baru;
                            $data_atasan_simpan[] = $data_atasan;
                        }
                    }
                    if(isset($data_atasan_simpan)) {
                    $this->m_operator->tambah_data_tabel_batch('formasi', $data_atasan_simpan);
                    }
                    
                //insert data bawahan
                    if (count($this->input->post('bawahan_lsg')) > 0) {
                        foreach ($this->input->post('bawahan_lsg') as $dt_bawahan) {
                            $data_bawahan['id_operator_atasan'] = $id_operator_baru;
                            $data_bawahan['id_operator_bawahan'] = $dt_bawahan;
                            $data_bawahan_simpan[] = $data_bawahan;
                        }
                    }
                    if(isset($data_bawahan_simpan)) {
                    $this->m_operator->tambah_data_tabel_batch('formasi', $data_bawahan_simpan);
                    }
                    
                header('location:' . site_url() . "operator/" . $this->index());
            } else {
                $data['list'] = $this->m_operator->get_all_operator();
                $data['konten'] = 'v_operator_tambah';
                $this->load->view('theme', $data);
            }
        } else {
            $this->session->set_flashdata('result_pass', 'NIK Operator harus berbeda');
            header('location:' . site_url('operator/tambah_operator'));
        }
    }

    function edit_operator() {
        $data['list'] = $this->m_operator->get_all_operator()->result();
        $id = $this->uri->segment(3);
        $data['operator'] = $this->m_operator->get_by_id($id)->row();

        foreach ($this->m_operator->get_data_formasi_atasan($data['operator']->id_operator)->result() as $data_id_atasan) {
            $data_ats[] = $data_id_atasan->id_operator_atasan;
        }
        
        $data['atasan'] = $data_ats;
        $data['nama_ats'] = $this->m_operator->get_data_formasi_atasan($data['operator']->id_operator);
        
        foreach ($this->m_operator->get_data_formasi_bawahan($data['operator']->id_operator)->result() as $data_id_bawahan) {
            $data_bwh[] = $data_id_bawahan->id_operator_bawahan;
        }
        
        $data['bawahan'] = $data_bwh;
        $data['nama_bwh'] = $this->m_operator->get_data_formasi_bawahan($data['operator']->id_operator);

        $data['konten'] = 'v_operator_edit';
//        echo '<pre>'.print_r($data['atasan']).'</pre>';
        $this->load->view('theme', $data);
    }

    function update_operator() {
        $data['nama'] = $_POST['nama'];
        $data['nik'] = $_POST['nik'];
        $data['jabatan'] = $_POST['jabatan'];
        $data['email'] = $_POST['email'];
        $data['username'] = $_POST['username'];
        $data['password'] = $_POST['password'];
        $data['no_hp'] = $_POST['no_hp'];
        $data['status'] = $_POST['status'];
        $res = $this->m_operator->update_operator($data, $_POST['id_operator']);
        if ($res) {
            //print_r($data);
            header('location:' . base_url() . "operator/" . $this->index());
        }


        //   if($this->form_validation->run() == TRUE) {
        //       print_r($data);
        //           // $res = $this->m_operator->update_operator($data);
        //           // header('location:'.site_url()."operator/".$this->index());
        //   } else {
        //     $data['konten'] = 'v_operator_edit';
        //     $this->load->view('theme', $data);
        // }
        // $cek_nik = $this->m_operator->cek_nik($data['nik']);
        // if($cek_nik == true){
        //     if($this->form_validation->run() == TRUE) {
        //         $res = $this->m_operator->update_operator($data);
        //         header('location:'.site_url()."operator/".$this->index());
        //     } else {
        //       $data['list'] = $this->m_operator->get_all_operator();
        //       $data['konten'] = 'v_operator_edit';
        //       $this->load->view('theme', $data);
        //     }
        // } else {
        //     $this->session->set_flashdata('result_pass', 'NIK Operator harus berbeda');
        //     header('location:'.site_url('operator/edit_operator'));
        // }
    }

    function edit_atasan() {
        if ($this->input->post() != NULL) {
            if (count($this->input->post('atasan_lsg')) > 0) {
                foreach ($this->input->post('atasan_lsg') as $dt_atasan) {
                    $data_atasan['id_operator_atasan'] = $dt_atasan;
                    $data_atasan['id_operator_bawahan'] = $this->input->post('id_operator');
                    $atasan[] = $this->m_operator->tambah_data_tabel('formasi', $data_atasan);
                }
                $data['get_atasan'] = $this->m_operator->atasan_get($atasan)->result_array();
            }
        }

        //$data['nama'] = $this->m_operator->get_data_formasi_atasan();
        $data['status'] = 'status';
        $this->output->set_output(json_encode($data));
    }

    function edit_bawahan() {
        if ($this->input->post() != NULL) {
            if (count($this->input->post('bawahan_lsg')) > 0) {
                foreach ($this->input->post('bawahan_lsg') as $dt_bawahan) {
                    $data_bawahan['id_operator_bawahan'] = $dt_bawahan;
                    $data_bawahan['id_operator_atasan'] = $this->input->post('id_operator');
                    $bawahan[] = $this->m_operator->tambah_data_tabel('formasi', $data_bawahan);
                }
                $data['get_bawahan'] = $this->m_operator->bawahan_get($bawahan)->result_array();
            }
        }

        //$data['nama'] = $this->m_operator->get_data_formasi_atasan();
        $data['status'] = 'status';
        $this->output->set_output(json_encode($data));
    }

    function hapus_operator($id_operator) {
        $res = $this->m_operator->delete_operator($id_operator);
        if ($res) {
            header('location:' . base_url() . "operator/" . $this->index());
        }
    }

    function hapus_formasi($id_formasi) {
        $res = $this->m_operator->delete_formasi($id_formasi);
        if ($res) {
            header('location:' . base_url() . "operator/edit_operator" . index_page());
        }
    }

    function peta($id_operator) {
        $this->load->library('googlemaps');

        $config['center'] = '37.4419, -122.1419';
        $config['zoom'] = 'auto';
        $this->googlemaps->initialize($config);

        $marker = array();
        $marker['position'] = '37.429, -122.1519';
        $marker['infowindow_content'] = '1 - Hello World!';
        $marker['icon'] = 'http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=A|9999FF|000000';
        $this->googlemaps->add_marker($marker);

        $marker = array();
        $marker['position'] = '37.409, -122.1319';
        $marker['draggable'] = TRUE;
        $marker['animation'] = 'DROP';
        $this->googlemaps->add_marker($marker);

        $marker = array();
        $marker['position'] = '37.449, -122.1419';
        $marker['onclick'] = 'alert("You just clicked me!!")';
        $this->googlemaps->add_marker($marker);
        $data['map'] = $this->googlemaps->create_map();

        $data['konten'] = 'v_peta';
        $this->load->view('theme', $data);
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
